<?php
class UsersController extends AppController
{
	var $name = 'Users';
    var $uses = array('User','Host','LoginAttempt');
    var $helpers = array('Javascript','DAuth');
    var $components = array('DAuth','RequestHandler');
    var $noReason = 'No reason given.';

    function login()
    {
        $error = '';
        if (!empty($this->data))
        {
             $success = $this->DAuth->attemptLogin($this->params['data'],$this->RequestHandler->getClientIP());
            if($success)
            {
                $this->DAuth->redirect('login_success');
                return true;
            }
            else
            {
                $error = $this->DAuth->getErrorMessage();
                if(!$error)
                {
                    $error = $this->noReason;
                }
            }
         }
        $this->DAuth->newSalt();
          $this->set('error', $error);
          $this->set('allowcleartext', $this->DAuth->allowClearText);
    }

    function register()
    {//FIXME: chri deze actie kan je gewoon outcommenten als je hem niet nodig hebt
        $error = '';
        if (!empty($this->data))
        {
             $success = $this->DAuth->attemptRegister($this->params['data'],$this->RequestHandler->getClientIP());
            if($success)
            {
                $this->DAuth->redirect('register_success');
                return true;
            }
            else
            {
                $error = $this->DAuth->getErrorMessage();
                if(!$error)
                {
                    $error = $this->noReason;
                }
            }
         }
        $this->set('error',$error);
        $this->set('allowcleartext', $this->DAuth->allowClearText);
    }

    function changePassword()
    {if(!$this->sessionUser) die();
        $error = '';
        if (!empty($this->data))
        {
             $success = $this->DAuth->attemptChangePassword($this->params['data'],$this->RequestHandler->getClientIP());
            if($success)
            {
                $this->DAuth->redirect('change_password_success');
                return true;
            }
            else
            {
                $error = $this->DAuth->getErrorMessage();
                if(!$error)
                {
                    $error = $this->noReason;
                }
            }
         }
        $this->set('error',$error);
        $this->set('allowcleartext', $this->DAuth->allowCleartext);
    }

    function logout()
    {if(!$this->sessionUser) die();
        $success = $this->DAuth->attemptLogout();
        if($success)
        {
            $this->Session->setFlash('Logout successfull');
            $this->DAuth->redirect('logout_success');
            return true;
        }
        else
        {
            $this->Session->setFlash('Logout failed');
            $this->DAuth->redirect('logout_failure');
            return true;
        }
    }
}
?>